package jlxy.ise.eroad.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.google.gson.Gson;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;
import jlxy.ise.eroad.R;
import jlxy.ise.eroad.activity.LoginActivity;
import jlxy.ise.eroad.base.BaseSwitchFragment;
import jlxy.ise.eroad.bean.ResetPwdInfo;
import jlxy.ise.eroad.bean.ServerRpsInfo;
import jlxy.ise.eroad.util.SingleToast;
import jlxy.ise.eroad.util.WebUtil;


/**
 * 忘记密码界面
 *
 * @author chen
 */
public class LoginFgtPwdFragment extends BaseSwitchFragment implements
        OnClickListener {

    private TextView mTxBack;
    private EditText emailTel;
    private EditText emailNumer;
    private Button findPasswordButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_findpwd, container,
                false);

        mTxBack = (TextView) rootView
                .findViewById(R.id.rl_top_btn_back);
        emailTel = (EditText) rootView
                .findViewById(R.id.editText_forget_phone);
        emailNumer = (EditText) rootView
                .findViewById(R.id.editText_forget_drivingnumber);

        findPasswordButton = (Button) rootView
                .findViewById(R.id.button_find_password);
        findPasswordButton.setOnClickListener(this);
        mTxBack.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_top_btn_back:
                mFragmentCallBack.fragmentCallBack(LoginActivity.JUMP_2_LOGIN, null);
                break;
            case R.id.button_find_password:
                //忘记密码，输入手机号和驾驶执照验证
                String phone = emailTel.getEditableText().toString();
                String number = emailNumer.getEditableText().toString();

                // 进行找回验证
                WebUtil.postForgetVerify(WebUtil.getRpForgetVerify(phone, number), new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        Gson gson = new Gson();
                        try {
                            ServerRpsInfo serverRpsInfo = gson.fromJson(response.toString(), ServerRpsInfo.class);
                            if (serverRpsInfo.getStatus().equals("ok") && serverRpsInfo.getMessage().equals("ok")) {
                                //获取信息成功
                                ResetPwdInfo resetPwdInfo = gson.fromJson(response.toString(), ResetPwdInfo.class);
                                if (!resetPwdInfo.getData().isEmpty()) {

                                    new MaterialDialog.Builder(mAct)
                                            .title("找回密码成功")
                                            .content("当前密码:\n" + resetPwdInfo.getData().get(0).getPasswd())
                                            .positiveText("我知道了")
                                            .build().show();
                                }else{
                                    SingleToast.showToast(mAct, "没有找到对应信息", 2000);
                                }
                            }
                        } catch (Exception e) {
                            SingleToast.showToast(mAct, "找回密码出错:" + e.getLocalizedMessage(), 2000);
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                        super.onFailure(statusCode, headers, throwable, errorResponse);
                        SingleToast.showToast(mAct, "找回密码失败", 2000);
                    }
                });


                break;

            default:
                break;
        }

    }

    public void doFragmentBackPressed() {
        mFragmentCallBack.fragmentCallBack(LoginActivity.JUMP_2_LOGIN, null);
    }

    ;

}
